package com.zyb.patient.activity;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.zyb.patient.R;
import com.zyb.patient.adapter.AboutMeClassAdapter;
import com.zyb.patient.dto.CourseDto;
import com.zyb.patient.view.swipemenulistview.SwipeMenu;
import com.zyb.patient.view.swipemenulistview.SwipeMenuCreator;
import com.zyb.patient.view.swipemenulistview.SwipeMenuItem;
import com.zyb.patient.view.swipemenulistview.SwipeMenuListView;

public class AboutMeClassActivity extends BaseActivity implements
		OnClickListener {

	// private List<ApplicationInfo> mAppList;

	private SwipeMenuListView mListView;
	private List<CourseDto> cList;
	private AboutMeClassAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_about_me_class);
		
		initViews();
		
		overridePendingTransition(R.anim.enter_righttoleft, R.anim.noanim);
	}

	private void initViews() {
		((TextView) findViewById(R.id.head_layout_tv)).setText("我的课程");
		findViewById(R.id.head_layout_back).setOnClickListener(this);

		// mAppList = getPackageManager().getInstalledApplications(0);
		mListView = (SwipeMenuListView) findViewById(R.id.listView);
		/** 模拟数据 **/
		cList = new ArrayList<CourseDto>();
		cList.add(new CourseDto(-1, "", "", "", "0", "1","0"));
		cList.add(new CourseDto(1, "标题一", "内容一", "www.baidu.com", "0", "0","1"));
		cList.add(new CourseDto(2, "标题二", "内容二", "www.baidu.com", "0", "1","1"));
		
		cList.add(new CourseDto(-1, "", "", "", "1", "1","0"));
		cList.add(new CourseDto(3, "标题三", "内容三", "www.baidu.com", "1", "1","1"));
		
		cList.add(new CourseDto(-1, "", "", "", "2", "1","0"));
		cList.add(new CourseDto(4, "标题四", "内容四", "www.baidu.com", "2", "1","1"));
		
//		String flag = cList.get(0).state;
//		for (CourseDto dto: cList){
//			if(dto.state.equals(flag)){
//				dto.isShowTop = "0";
//			}else{
//				dto.isShowTop = "1";
//			}
//		}
		
		
		mAdapter = new AboutMeClassAdapter(mContext, cList);
		mListView.setAdapter(mAdapter);

		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// // create "open" item
				// SwipeMenuItem openItem = new SwipeMenuItem(
				// getApplicationContext());
				// // set item background
				// openItem.setBackground(new ColorDrawable(Color.rgb(0xC9,
				// 0xC9,
				// 0xCE)));
				// // set item width
				// openItem.setWidth(dp2px(90));
				// // set item title
				// openItem.setTitle("Open");
				// // set item title fontsize
				// openItem.setTitleSize(18);
				// // set item title font color
				// openItem.setTitleColor(Color.WHITE);
				// // add to menu
				// menu.addMenuItem(openItem);

				// create "delete" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				// set item width
				deleteItem.setWidth(dp2px(90));
				// set a icon
				deleteItem.setIcon(R.drawable.ic_delete);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		mListView.setMenuCreator(creator);

		// step 2. listener item click event
		mListView
				.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
					@Override
					public boolean onMenuItemClick(int position,
							SwipeMenu menu, int index) {
						switch (index) {
						case 0:
							cList.remove(position);
							mAdapter.notifyDataSetChanged();
							break;
						default:
							break;
						}
						return false;
					}
				});

		// // set SwipeListener
		// mListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener()
		// {
		//
		// @Override
		// public void onSwipeStart(int position) {
		// // swipe start
		// }
		//
		// @Override
		// public void onSwipeEnd(int position) {
		// // swipe end
		// }
		// });
		//
		// // set MenuStateChangeListener
		// mListView.setOnMenuStateChangeListener(new
		// SwipeMenuListView.OnMenuStateChangeListener() {
		// @Override
		// public void onMenuOpen(int position) {
		// }
		//
		// @Override
		// public void onMenuClose(int position) {
		// }
		// });
		//
		// // other setting
		// // listView.setCloseInterpolator(new BounceInterpolator());
		//
		// // test item long click
		// mListView.setOnItemLongClickListener(new
		// AdapterView.OnItemLongClickListener() {
		//
		// @Override
		// public boolean onItemLongClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// Toast.makeText(getApplicationContext(), position + " long click",
		// Toast.LENGTH_SHORT).show();
		// return false;
		// }
		// });
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view.getId() == R.id.head_layout_back) {
			super.onBackPressed();
		}
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

}
